﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using BinLibrary;
using BinLibrary.WinHelper;
using RevitReadAutoCAD.DotNetRevit;
using RevitReadAutoCAD.TeighaHelper;
using Teigha.Core;
using Teigha.TD;

namespace RevitReadAutoCAD
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    [Journaling(JournalingMode.UsingCommandData)]

    class RunCmd : IExternalCommand
    {
        public readonly static string Workpath = @"D:\Program Files\Hongye\HYMEI2018\bin";

        static RunCmd()
        {
            //AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
        }

        static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            //LogHelper.LogWrite(@"c:\LackOfDlls.txt", args.Name);
            MessageBox.Show("resoleving......");

            LogHelper.LogWrite(@"c:\OnAssemblyResolve_nonExist.txt", args.RequestingAssembly.GetName().Name);

            var file = string.Empty;
            var assemblyName = new AssemblyName(args.Name);
            file = Path.Combine(Workpath, string.Format("{0}.dll", assemblyName.Name));
            if (File.Exists(file))
            {
                LogHelper.LogWrite(@"c:\OnAssemblyResolve.txt", file);
                return Assembly.LoadFile(file);
            }
            return args.RequestingAssembly;
        }

        private static void AddEnvironmentPaths(params string[] paths)
        {
            var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
            var newpath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths));
            Environment.SetEnvironmentVariable("PATH", newpath);
        }
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection sel = uidoc.Selection;
            string filepath = @"C:\Users\XU\Desktop\2F消火栓.dwg";
            filepath = @"C:\Users\XU\Desktop\消火栓1.dwg";





            var refence1 = sel.PickObject(ObjectType.Element);
            var importinstance = doc.GetElement(refence1) as ImportInstance;
            var cadlinktype = doc.GetElement(importinstance.GetTypeId()) as CADLinkType;
            cadlinktype.GetExternalFileReference().GetAbsolutePath();
            filepath = importinstance.GetCadPath();

           
            //string filepath1 = @"C:\Users\XU\Desktop\xhs.dwg";

           

          

            //MessageBox.Show(filepath);

            //var services = new ExSystemServices();
            //TD_Db.odInitialize(services);

            //var appservices = new ExHostAppServices();

            //appservices.readFile(filepath);

            Utils.GetCADTextInfl1(filepath);


            return Result.Succeeded;
        }
    }
}
